package com.yidu.qianfeng.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yidu.qianfeng.common.bean.company.*;
import com.yidu.qianfeng.service.EmployeeServiceInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 田舒蓉
 * 员工管理 分站管理 员工页面模块
 */

@Controller
public class EmployeeController {

    @Autowired
    EmployeeServiceInter serviceInter;

    /**
     * 查询新增员工页面的权限模块
     * @return
     */
    @RequestMapping("/pageModel")
    @ResponseBody
    public List<EmpPage> queryPageModel(){
        List<EmpPage> empPages = serviceInter.queryEmpPage();
        return empPages;
    }


    /**
     * 员工管理页面
     * @return
     */
    @RequestMapping("/employee")
    public String employee(){
        return "staff/employeeAll";
    }

    /**
     * 查询员工
     * @return
     */
    @RequestMapping("/empAdmin")
    @ResponseBody
    public Map<String, Object> employeeAll(){
        List<EmpInfo> empInfoList = serviceInter.queryEmpInfo();
        Map<String, Object> map=new HashMap<String, Object>();
        map.put("code", "0");
        map.put("msg", "");
        map.put("count",serviceInter.queryEmpInfo());
        map.put("data", empInfoList);
        return map;
    }

    @RequestMapping("/addEmp")
    public String addEmpPage(){
        return "staff/addEmployee";
    }

    /**
     * 新增员工页面
     * @param empInfo 员工信息
     * @param empPageAuthIds  员工页面权限
     * @param empModelAuthIds  员工模块权限
     * @return 员工
     */
    @RequestMapping("/saveEmp")
    @ResponseBody
    public int addEmployee(String empInfo,String empPageAuthIds, String empModelAuthIds){
        //获取jackson的常用工具类
        ObjectMapper mapper = new ObjectMapper();

        //员工实体，页面权限id列表，模块权限id列表
        EmpInfo emp = null;
        List<String> empPageAuthIdsList = null;
        List<String> empModelAuthIdsList = null;

        try {
            //jsonStr转实体对象
            emp =  mapper.readValue(empInfo, EmpInfo.class);
            empPageAuthIdsList = mapper.readValue(empPageAuthIds, new TypeReference<List<String>>(){});
            empModelAuthIdsList = mapper.readValue(empModelAuthIds, new TypeReference<List<String>>(){});

        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        //新增员工信息
        Integer employee = serviceInter.saveEmployee(emp, empPageAuthIdsList, empModelAuthIdsList);
        return employee;
    }

    /**
     * 修改员工页面
     * @param empInfo
     * @return
     */
    @RequestMapping("/updateEmpPage")
    public String updateEmp(EmpInfo empInfo){
        return "staff/updateEmp";
    }

    /**
     *  修改员工信息的方法
     * @param empInfo 员工信息
     * @param empPageAuthIds 员工页面权限
     * @param empModelAuthIds 员工模块权限
     * @return 员工
     */

    public int updateEmp(String empInfo,String empPageAuthIds, String empModelAuthIds){
        //获取jackson的常用工具类
        ObjectMapper mapper = new ObjectMapper();

        //员工实体，页面权限id列表，模块权限id列表
        EmpInfo emp = null;
        List<String> empPageAuthIdsList = null;
        List<String> empModelAuthIdsList = null;

        try {
            //jsonStr转实体对象
            emp =  mapper.readValue(empInfo, EmpInfo.class);
            empPageAuthIdsList = mapper.readValue(empPageAuthIds, new TypeReference<List<String>>(){});
            empModelAuthIdsList = mapper.readValue(empModelAuthIds, new TypeReference<List<String>>(){});

        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        //新增员工信息
        Integer employee = serviceInter.updateEmployee(emp, empPageAuthIdsList, empModelAuthIdsList);
        return employee;
    }

    /**
     * 根据员工账号判断员工是否存在
     * @param empAccount 员工账号
     * @return
     */
    @RequestMapping("/queryByEmpId")
    @ResponseBody
    public Boolean queryByEmpId(String empAccount){
        EmpInfo account = serviceInter.querybyEmpId(empAccount);
        if (account == null){
            return true;
        }else {
            return false;
        }
    }
    /**
     * 根据员工账号查询员工
     * @param empAccount 员工账号
     * @return
     */
    @RequestMapping("/queryByEmpAccount")
    @ResponseBody
    public EmpInfo queryByEmpAccount(String empAccount){
        EmpInfo account = serviceInter.querybyEmpId(empAccount);
        return account;
    }

    /**
     *  删除员工
     * @param empAccount 员工账号
     * @return
     */
    @RequestMapping("/deleteEmp")
    @ResponseBody
    public int deleteEmp(String empAccount){
        int removeEmp = 0;
        for (String all : empAccount.split(",")) {
            removeEmp = serviceInter.deleteEmp(all);
        }
        return removeEmp;
    }

    /**
     * 新增分站页面
     * @return
     */
    @RequestMapping("/addSubPage")
    public String addSubPage(){
        return "staff/addSub";
    }

    /**
     * 新增分站功能
     * @param subst 分站
     * @return
     */
    @RequestMapping("/addSub")
    @ResponseBody
    public int addSubstation(Subst subst){
        subst.setSubstId(String.valueOf(UUID.randomUUID()).substring(0,8));
        int count = serviceInter.saveSubst(subst);
        return  count;
    }

    /**
     * 分站管理页面
     * @return
     */
    @RequestMapping("/subAll")
    public String substation(){
        return "staff/substation";
    }

    /**
     * 查询分站
     * @return
     */
    @RequestMapping("/querySub")
    @ResponseBody
    public Map<String, Object> subAll(){
        List<Subst> sub = serviceInter.querySubst();
        Map<String, Object> map=new HashMap<String, Object>();
        map.put("code", "0");
        map.put("msg", "");
        map.put("count",sub);
        map.put("data", sub);
        return map;
    }


    /**
     * 删除分站
     * @return
     * @param substId 分站id
     */
    @RequestMapping("/removeSub")
    @ResponseBody
    public int removeSub(String substId) {
        int removeSub =0;
        for (String all : substId.split(",")) {
            removeSub = serviceInter.removeSub(all);
        }
        return removeSub;
    }

    @RequestMapping("/updadeSub")
    public String updadeSub(){
        return "staff/updateSub";
    }

    /**
     * 根据id查询分站
     * @return
     */
    @RequestMapping("/queryById")
    @ResponseBody
    public Subst queryById(String substId){
        return serviceInter.queryById(substId);
    }

    /**
     * 修改分站
     * @param subst
     * @return
     */
    @RequestMapping("/updateSubst")
    @ResponseBody
    public int updateSubst(Subst subst){
        return serviceInter.updateSubst(subst);
    }
}
